package org.kunlong.qgen.boot;

import java.io.File;
import java.io.FilenameFilter;
import java.util.LinkedList;
import java.util.List;

import org.kunlong.qgen.common.util.ConfigurationManager;
import org.kunlong.qgen.common.util.QuestionManager;
import org.kunlong.qgen.common.util.TemplateManager;
import org.kunlong.qgen.domain.work.QuestionaireRequest;
import org.kunlong.qgen.parser.RequestParser;
import org.kunlong.qgen.service.QuestionLoadService;
import org.kunlong.qgen.service.impl.DefaultMultiThreadLoadService;

/**
 * The program starts here
 * 
 * @author Kent_G
 * */
public class Boot {
	// the place to load configuration
	private static final String CONF_FILE_NAME = "/conf/settings.xml";
	private static final String QUESTION_REQUEST_CONF_FILE = "/conf/generate_config.xml";
	// the directory that the program is running
	public static final String CURRENT_DIRECTORY = System
			.getProperty("user.dir");

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("Root:[" + CURRENT_DIRECTORY + "]");
		String configPath = CURRENT_DIRECTORY + CONF_FILE_NAME;
		File file = new File(configPath);
		try {
			// initialize the configuration
			ConfigurationManager.initConfiguration(file);
		} catch (Exception e) {
			// any exception, just quit the system
			System.out.println("Error Intialling Configuration File");
			e.printStackTrace();
			System.exit(-1);
		}

		try {
			// load the template used to print
			TemplateManager.loadTemplate();
		} catch (Exception e) {
			System.out.println("Error Loading Template File");
			e.printStackTrace();
			System.exit(-1);
		}
		String dest_dir_path = "C:\\test";
		File dest_dir = new File(dest_dir_path);
		vaidateTargetDirectory(dest_dir);
		List<File> questionXMLFiles = findAllQuestionXML();
		QuestionLoadService loadService = new DefaultMultiThreadLoadService();
		// load all the question to a questionholder
		loadService.loadFromXML(questionXMLFiles,
				QuestionManager.getQuestionHolder());
		File requestFile = new File(QUESTION_REQUEST_CONF_FILE);
		QuestionaireRequest request = null;
		try {
			request = RequestParser.parse(requestFile);
		} catch (Exception ex) {
			ex.printStackTrace();
			System.out.println("Request Invalid!!");
			System.exit(-1);
		}
		
	}

	/**
	 * method that find all the Question File in directories
	 * */
	private static List<File> findAllQuestionXML() {
		List<File> questionFilesDirectory = new LinkedList<File>();
		List<File> questionXMLFiles = new LinkedList<File>();
		for (String path : ConfigurationManager.getConfiguration()
				.getQuestionDirs()) {
			File questionFileDirectory = new File(path);
			// if it is a directory
			if (questionFileDirectory.isDirectory()) {
				questionFilesDirectory.add(questionFileDirectory);
			}
		}

		// just filter the name
		FilenameFilter filter = new FilenameFilter() {
			public boolean accept(File dir, String name) {
				if (name.contains("question.xml")) {
					return true;
				} else {
					return false;
				}
			}
		};
		for (File directory : questionFilesDirectory) {
			File[] allFile = directory.listFiles(filter);
			for (File f : allFile) {
				questionXMLFiles.add(f);
			}
		}
		System.out.println("Find Total Question Files:["
				+ questionXMLFiles.size() + "]");
		return questionXMLFiles;
	}

	/**
	 * validate target directory,if fail just quit
	 * */
	private static void vaidateTargetDirectory(File dest_dir) {
		if (!dest_dir.exists()) {
			// if directory does not exist,create a new one
			dest_dir.mkdir();
		} else if (!dest_dir.isDirectory()) {
			// if the directory is not a directory, just quit
			System.out.println(dest_dir.getName() + " is not a directory!!!");
			System.exit(-1);
		}
	}

}
